import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../helpers/magma_color.dart';
import '../helpers/magma_text_style.dart';

/// 举报选项枚举
enum ReportOption {
  inappropriateContent('Inappropriate Content'),
  hateSpeech('Hate Speech'),
  disturbingContent('Disturbing Content'),
  illegalActivity('Illegal Activity'),
  spamAdvertisement('Spam/Advertisement'),
  otherIssues('Other Issues'),
  blockUser('Block User');

  const ReportOption(this.displayName);
  final String displayName;
}

/// 举报对话框
class MagmaReportDialog {
  /// 显示举报对话框
  static Future<void> showReportDialog(
    BuildContext context, {
    required String title,
    required String subtitle,
    required Function(ReportOption) onReportSubmitted,
  }) {
    return showDialog(
      context: context,
      barrierDismissible: true,
      builder:
          (context) => _ReportDialogContent(
            title: title,
            subtitle: subtitle,
            onReportSubmitted: onReportSubmitted,
          ),
    );
  }
}

/// 举报对话框内容
class _ReportDialogContent extends StatefulWidget {
  final String title;
  final String subtitle;
  final Function(ReportOption) onReportSubmitted;

  const _ReportDialogContent({
    required this.title,
    required this.subtitle,
    required this.onReportSubmitted,
  });

  @override
  State<_ReportDialogContent> createState() => _ReportDialogContentState();
}

class _ReportDialogContentState extends State<_ReportDialogContent> {
  ReportOption? _selectedOption;

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 24.w),
        decoration: BoxDecoration(
          color: MagmaColor.black,
          borderRadius: BorderRadius.circular(16.r),
          border: Border.all(
            color: MagmaColor.white.withValues(alpha: 0.2),
            width: 1.w,
          ),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 标题区域
            _buildHeaderSection(),
            // 举报选项列表
            _buildReportOptionsList(),
            // 底部按钮
            _buildBottomButtons(),
          ],
        ),
      ),
    );
  }

  /// 构建标题区域
  Widget _buildHeaderSection() {
    return Container(
      padding: EdgeInsets.all(24.w),
      child: Column(
        children: [
          Text(widget.title, textAlign: TextAlign.center).blackHanSans(
            fontSize: 20.sp,
            color: MagmaColor.white,
            fontWeight: FontWeight.w600,
          ),
          SizedBox(height: 8.h),
          Text(widget.subtitle, textAlign: TextAlign.center).inter(
            fontSize: 14.sp,
            color: MagmaColor.white.withValues(alpha: 0.7),
            fontWeight: FontWeight.w400,
          ),
        ],
      ),
    );
  }

  /// 构建举报选项列表
  Widget _buildReportOptionsList() {
    return Column(
      children:
          ReportOption.values.map((option) {
            return _buildReportOptionItem(option);
          }).toList(),
    );
  }

  /// 构建单个举报选项
  Widget _buildReportOptionItem(ReportOption option) {
    final isSelected = _selectedOption == option;

    return GestureDetector(
      onTap: () {
        setState(() {
          _selectedOption = option;
        });
      },
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h),
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
        decoration: BoxDecoration(
          color:
              isSelected
                  ? MagmaColor.purple.withValues(alpha: 0.2)
                  : Colors.transparent,
          borderRadius: BorderRadius.circular(8.r),
          border: Border.all(
            color:
                isSelected
                    ? MagmaColor.purple
                    : MagmaColor.white.withValues(alpha: 0.1),
            width: 1.w,
          ),
        ),
        child: Row(
          children: [
            Container(
              width: 20.w,
              height: 20.w,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                border: Border.all(
                  color:
                      isSelected
                          ? MagmaColor.purple
                          : MagmaColor.white.withValues(alpha: 0.3),
                  width: 2.w,
                ),
                color: isSelected ? MagmaColor.purple : Colors.transparent,
              ),
              child:
                  isSelected
                      ? Icon(Icons.check, color: MagmaColor.white, size: 12.sp)
                      : null,
            ),
            SizedBox(width: 12.w),
            Expanded(
              child: Text(option.displayName).inter(
                fontSize: 14.sp,
                color: MagmaColor.white,
                fontWeight: FontWeight.w400,
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 构建底部按钮
  Widget _buildBottomButtons() {
    return Container(
      padding: EdgeInsets.all(16.w),
      child: Row(
        children: [
          // 取消按钮
          Expanded(
            child: GestureDetector(
              onTap: () => Navigator.of(context).pop(),
              child: Container(
                height: 44.h,
                decoration: BoxDecoration(
                  color: MagmaColor.white.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(8.r),
                  border: Border.all(
                    color: MagmaColor.white.withValues(alpha: 0.2),
                    width: 1.w,
                  ),
                ),
                child: Center(
                  child: Text('Cancel').inter(
                    fontSize: 14.sp,
                    color: MagmaColor.white,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
          ),
          SizedBox(width: 12.w),
          // 举报按钮
          Expanded(
            child: GestureDetector(
              onTap: _selectedOption != null ? _submitReport : null,
              child: Container(
                height: 44.h,
                decoration: BoxDecoration(
                  color:
                      _selectedOption != null
                          ? MagmaColor.purple
                          : MagmaColor.grey.withValues(alpha: 0.3),
                  borderRadius: BorderRadius.circular(8.r),
                ),
                child: Center(
                  child: Text('Report').inter(
                    fontSize: 14.sp,
                    color: MagmaColor.white,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 提交举报
  void _submitReport() {
    if (_selectedOption != null) {
      Navigator.of(context).pop();
      widget.onReportSubmitted(_selectedOption!);
    }
  }
}
